package com.yanghui.dtracing.plugin.redis;

import com.yanghui.dtracing.core.constant.TypeEnum;
import com.yanghui.dtracing.core.opentracing.DTracerTags;
import com.yanghui.dtracing.core.plugin.interceptor.AbstractInstanceMethodInterceptor;
import com.yanghui.dtracing.core.plugin.interceptor.MethodInterceptResult;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Method;

public class JedisInstanceMethodInterceptor extends AbstractInstanceMethodInterceptor {

    @Override
    protected void doBeforeMethod(Object thisObj, Method method, Object[] allArguments, MethodInterceptResult result, Span span) throws Throwable {
        Jedis jedis = (Jedis)thisObj;
        String host = jedis.getClient().getHost() + ":" + jedis.getClient().getPort();

        span.setTag(Tags.DB_TYPE, DTracerTags.DB_TYPE_REDIS)
            .setTag(Tags.DB_INSTANCE,host)
            .setTag(Tags.COMPONENT,DTracerTags.COMPONENT_JEDIS)
            .setTag(DTracerTags.TYPE, TypeEnum.EXIT.getName());
    }
}
